//
//  LDCellCollectionView.swift
//  Weibo201655
//
//  Created by pro on 16/5/11.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit

//MARK:----------------------- 私有全局变量 ----------------------------------//
//最大列数
private let MAXCOLs = 3
// cell之间的间距
private let cellMargin: CGFloat = 10
//cell宽高
private let cellWH = (UIScreen.width - 2 * LDStatusCellMargin - CGFloat(MAXCOLs - 1) * cellMargin) / CGFloat(MAXCOLs)
//cell的size
private let cellSize = CGSizeMake(cellWH, cellWH)

class LDCellCollectionView: UICollectionView {
    
    //MARK:----------------------- 属性 ----------------------------------//
    var status: LDStatus? {
        didSet {
            //重新计算size
            let newSize = calculateSize()
            //更新约束
            snp_updateConstraints { (make) -> Void in
                make.size.equalTo(newSize)
            }
            //刷新
            self.reloadData()
        }
    }
    let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    let cellID = "pictureCellID"
    //MARK:----------------------- 方法 ----------------------------------//
    init() {
        super.init(frame: CGRectZero, collectionViewLayout: flowLayout)
        
        //MARK: 代理
        self.dataSource = self
        self.delegate = self
        
        //MARK: 注册cell
        registerClass(LDPictureCell.self, forCellWithReuseIdentifier: cellID)
        
        backgroundColor = UIColor.whiteColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func calculateSize()->CGSize {
        let count = status?.pictureUrls?.count ?? 0
        
        if count == 0 {
            
            return CGSizeZero
        }
        
        if count == 1 {
            let size = CGSizeMake(180, 120)
            //itemSize
            flowLayout.itemSize = size
            return size
        }
        
        //设置cell基本信息
        flowLayout.itemSize = cellSize
        flowLayout.minimumInteritemSpacing = cellMargin
        flowLayout.minimumLineSpacing = cellMargin
        
        var col = MAXCOLs
        if count == 2 || count == 4 {
            col = 2
        }
        //计算collectionview的宽高
        let row = count / col
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * cellMargin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * cellMargin
        //print("\(count) \(col)  \(row)   \(w)  \(h)  \(cellWH)")
        return CGSizeMake(w, h)
        
    }

}
//MARK:----------------------- 数据源和代理方法 ----------------------------------//
extension LDCellCollectionView: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pictureUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID, forIndexPath: indexPath) as! LDPictureCell
        cell.imgURL = status?.pictureUrls![indexPath.item]

        return cell
    }
    
}

//MARK:----------------------- LDPictureCell类 ----------------------------------//
class LDPictureCell: UICollectionViewCell {
    
    var imgURL: NSURL? {
        didSet {
            //显示图片
            picImgView.sd_setImageWithURL(imgURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(picImgView)
        picImgView.contentMode = UIViewContentMode.ScaleAspectFill
        picImgView.layer.masksToBounds = true
        
        picImgView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var picImgView: UIImageView = UIImageView(image: nil)
}
